<template>
	<view class="page-containter" :style="{'height':winHeight}">
		<view class="login-container">
			<image src="../../static/app/icon_app.png"></image>
			<view class="button-container">
				<button class="cancel" @click="clickCancel">暂不登录</button>
				<button class="ok" v-if="isCanUseUserProfile" @click="bindWxLogin">微信登录</button>
				<button class="ok" v-else open-type="getUserInfo" @getuserinfo="bindWxLogin">微信登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	const APP = getApp();
	
	export default {
		data() {
			return {
				winHeight: "500px",
				isCanUseUserProfile:true,//新功能
				//微信信息
				dataWxJsCode:'',
				dataWxRawData:'',
				dataWxSignature:'',
				dataWxEncryptedData:'',
				dataWxIv:'',
				//声明变量
				dataBackFlag:0,//返回继续操作（选项：0》不返回；1》仅返回；2》返回且刷新；）
			}
		},
		methods: {
			doLogin() {
				let _this = this;
				
				APP._post('接口', {
					code: _this.dataWxJsCode,
					rawData: _this.dataWxRawData,
					signature: _this.dataWxSignature,
					encryptedData: _this.dataWxEncryptedData,
					iv: _this.dataWxIv,
				}, null, function(res) {
					//执行成功
					if (res.status === APP._appconfig().fwcode.successed) {
						let data = res.data;
						let cacheData = {
							user_id:data.userid,
							user_token:data.auth_token,
						};
						APP._appauth().setUserCacheData(cacheData);
						 _this.doNavigateUrl();
					} else {
						APP._apptool().showToastInfo(res.message);
					}
				}, function(res) {
					//执行失败
					_this.doWxLogin(function(){
						_this.doLogin();
					});
				}, function(res) {
					//执行完成
				});
			},
			doWxLogin(callback){
				let _this = this;
				
				uni.login({
					provider: 'weixin',
					timeout:10000,
					success(res) {
						_this.dataWxJsCode = res.code;
						callback && callback();
					},fail(res) {
						APP._applog().error(res);
					},complete(res) {
						//执行完成
					}
				});
			},
			doWxChkSession(){
				let _this = this;
				
				uni.checkSession({
					success(res) {
						// session_key 未过期，并且在本生命周期一直有效
						if(_this.dataWxJsCode.length>0){
							_this.doLogin();
						}else{
							_this.doWxLogin(function(){
								_this.doLogin();
							});
						}
					},fail(res) {
						// session_key 已经失效，需要重新执行登录流程
						_this.doWxLogin(function(){
							_this.doLogin();
						});
					},complete(res) {
						//执行完成
					}
				});
			},
			bindWxLogin(){
				let _this = this;
				
				if(_this.isCanUseUserProfile){
					uni.getUserProfile({
						desc: '用于完善会员资料',
						success(res) {
							let userInfo = res.userInfo;
							_this.dataWxRawData = res.rawData;
							_this.dataWxSignature = res.signature;
							_this.dataWxEncryptedData = res.encryptedData;
							_this.dataWxIv = res.iv;
							_this.doWxChkSession();
						},fail(res) {
							APP._applog().error(res);
						},complete(res) {
							//执行完成
							APP._applog().info(res);
						}
					});
				}else{
					uni.getUserInfo({
						lang:'zh_CN',
						success(res) {
							let userInfo = res.userInfo;
							_this.dataWxRawData = res.rawData;
							_this.dataWxSignature = res.signature;
							_this.dataWxEncryptedData = res.encryptedData;
							_this.dataWxIv = res.iv;
							_this.doWxChkSession();
						},fail(res) {
							APP._applog().error(res);
						},complete(res) {
							//执行完成
							APP._applog().info(res);
						}
					});
				}
			},
			//跳转控制
		  doNavigateUrl:function(){
			let _this = this;
			
			if(_this.dataBackFlag>0){
			  APP._apptool().showToastInfo('登录成功，请继续操作！', null, 2000, function (res) {
				APP._apptool().navigateBackPage(1, function (targetPage) {
				  if(2 == _this.dataBackFlag){
					//需刷新数据面，必须实现refreshPage()方法
					targetPage.$vm.refreshPage();
				  }
				});
			  });
			}else{
			  uni.reLaunch({
			  	url: '/pages/index',
			  });
			}
		  },
			clickCancel(){
				let _this = this;
				
				uni.navigateBack({
					delta:1,
				})
			},
		},
		onLoad(options) {
			let _this = this;
			
			//设置界面高度
			uni.getSystemInfo({
				success(res) {
					_this.winHeight = res.windowHeight+"px";
				}
			})
			
			//刷新标识
			if(options.refresh){
			  //刷新标识：refresh=1
			  _this.dataBackFlag = parseInt(options.refresh);
			}
			
			_this.isCanUseUserProfile = uni.getUserProfile;
		},
		onShow(event){
			this.doWxLogin(null);
		},
	}
</script>

<style lang="scss">
	.page-containter{
		width: 100%;
		display: flex; display: -webkit-flex; align-items: center; justify-content: center;
		background-color: #FFFFFF;
		.login-container{
			display: flex; display: -webkit-flex; flex-direction: column; align-items: center;
			padding-bottom: 200rpx;
			image{
				width: 350rpx; height: 350rpx;
			}
			.button-container{
				width: 600rpx; margin-top: 100rpx;
				display: flex; display: -webkit-flex; flex-direction: row; align-items: center; justify-content: space-between;
				button{
					width: 45%;
					font-size: 30rpx;
				}
				.cancel{
					background-color: #DFDFDF;
				}
				.ok{
					background-color: #FF0000; color:#FFFFFF;
				}
			}
		}
	}
</style>
